//index.js
//获取应用实例
//初始化云
wx.cloud.init();
const db = wx.cloud.database();
//获取数据库操作命令
const command = db.command;
const app = getApp()

Page({
  data: {
    swiperImg:[
      "https://6265-beibei-elvzp-1302660026.tcb.qcloud.la/img/swiper/untitled0.png?sign=21903243c28aa944ebb5285532299921&t=1598848293",
      "https://6265-beibei-elvzp-1302660026.tcb.qcloud.la/img/swiper/untitled1.png?sign=b8fa0983c611a303b06f82f739993b0d&t=1598848303",
      "https://6265-beibei-elvzp-1302660026.tcb.qcloud.la/img/swiper/untitled2.png?sign=9505e594bd7b494b246fbdc9c840b479&t=1598848312",
      "https://6265-beibei-elvzp-1302660026.tcb.qcloud.la/img/swiper/untitled3.png?sign=9013a7cfae92dd0de960588f192081fa&t=1598848321",
      "https://6265-beibei-elvzp-1302660026.tcb.qcloud.la/img/swiper/untitled4.png?sign=74dad79cebf8664466f5bacf79048c81&t=1598848329",
    ],
    navItem:[
      {
        link:"../popular/popular",
        text:"人气热销"
      },{
        link:"../new/new",
        text:"新品"
      },{
        link:"../starFactory/starFactory",
        text:"星工场"
      },{
        link:"../sixDiscount/sixDiscount",
        text:"6折"
      },{
        link:"../flowerWords/flowerWords",
        text:"花语程行"
      },{
        link:"../stars/stars",
        text:"星系列"
      }
    ],
    product:Array,
    activityProduct:Array,
    now:Date,
    productItem:[
      {
        link:"",
        text:"全部"
      },{
        link:"",
        text:"新品"
      },{
        link:"",
        text:"6折优惠"
      },{
        link:"",
        text:"星工场"
      },{
        link:"",
        text:"星系列"
      },{
        link:"",
        text:"花语程行"
      },{
        link:"",
        text:"迷你联萌"
      }
    ],
    navSelected:"6折优惠",
    productNav:Array
  },
  //页面加载执行函数
  onLoad: function () {
    // 获取新品上线栏数据
    this.getProduct()
    // 获取活动栏商品数据
    this.getActivityProduct()
    // 获取底部导航商品数据
    this.getProductNav(1,true)
    // 定时器：获取当前时间
    this.timmer = setInterval(()=>{
      this.setData({
        now:new Date().getTime()
      })
    },1000) 
  },
  //页面卸载执行清楚定时器
	onUnload: function () {
    window.clearInterval(this.timmer)
    console.log("删除定时器")
    
	},
  //用户下滑页面时触发刷新
  onPullDownRefresh: function () {
    console.log("aa")
    this.onLoad();
    wx.stopPullDownRefresh()
	},
  // 获取新品上线栏数据
  getProduct(){
    var collection = db.collection('product');
    collection.field({
      _id:false,
      id:true,
      img:true,
      price:true,
      new:true,
      title:true,
    }).where({
      pubtime:command.gte(new Date().getTime()-2592000000)
    }).get().then(res=>{
      
      this.setData(
        {product : res.data}
      )
    }).catch(err=>{
      console.log(err)
    })
  },
  // 获取活动栏商品数据
  getActivityProduct(){
    var collection = db.collection('product');
    collection.field({
      _id:false,
      id:true,
      img:true,
      title:true,
      price:true,
      discountTime:true,
      discount:true,
      stock:true,
      sold:true,
      content:true,
    }).where({
      active:true
    }).get().then(res=>{
      this.setData({
        activityProduct:res.data,
      })
    }).catch(err=>{
      console.log(err)
    })
  },
  // 底部导航获取商品
  getProductNav(event,first=false){
    if(first){
      var navText = "6折优惠"
    }else{
      var navText = event.currentTarget.dataset.text;
    }
    var collection = db.collection('product');
    // 更改当前选项卡样式
    this.setData({
      navSelected:navText
    })
    if(navText == "全部"){
      collection.where({
        id:command.gte(1)
      }).field({
        _id:false,
        id:true,
        img:true,
        title:true,
        price:true
      }).get().then(res=>{
        this.setData({
          productNav:res.data
        })
      })
    }else if(navText == "新品"){
      collection.where({
        pubtime:command.gte(this.data.now-2592000000)
      }).field({
        _id:false,
        id:true,
        img:true,
        title:true,
        price:true
      }).get().then(res=>{
        this.setData({
          productNav:res.data
        })
      })
    }else if(navText == "6折优惠"){
      collection.where({
        discount:command.eq(6)
      }).field({
        _id:false,
        id:true,
        img:true,
        title:true,
        price:true
      }).get().then(res=>{
        this.setData({
          productNav:res.data
        })
      }).catch(err=>{
        console.log(err)
      })
    }else if(navText == "星工场" || navText == "星系列" || navText == "花语程行" || navText == "迷你联萌"){
      collection.where({
        serious:command.eq(navText)
      }).field({
        _id:false,
        id:true,
        img:true,
        title:true,
        price:true
      }).get().then(res=>{
        this.setData({
          productNav:res.data
        })
      }).catch(err=>{
        console.log(err)
      })
    }else{
      console.log("暂未开发")
    }
  },
  goDetail(prop){
    app.goDetail(prop)
  }
})
